package g4p.newg4p;

import g4p_controls.G4P;
import g4p_controls.GAlign;
import g4p_controls.GButton;
import g4p_controls.GCheckbox;
import g4p_controls.GControlMode;
import g4p_controls.GEvent;
import g4p_controls.GKnob;
import g4p_controls.GLabel;
import g4p_controls.GSlider;
import g4p_controls.GSlider2D;
import g4p_controls.GToggleControl;
import g4p_controls.GValueControl;
import g4p_controls.GValueControl2D;
import processing.core.PApplet;

public class Slider2D_test extends PApplet {
	GSlider2D s0;
	

	public void setup(){
		size(400,340);
		
		s0 = new GSlider2D(this, 40, 100, 160, 110);
//		s0.setXlimits(6, 1, 100);
//		s0.setYlimits(-20.0f, -100, -10);
		makeSliderConfigControls();
		lblValueX.setText("" + s0.getValueXF());
		lblValueY.setText("" + s0.getValueYF());
		
	}
	
	public void draw(){
		background(bgcol);
		fill(227,230,255);
		noStroke();
		rect(width - 140, 0, 140, height);
		rect(0, height - 40 , width - 140, 40);
	}
	
	public void handleSlider2DEvents(GSlider2D slider2d, GEvent event) { 
		if(event == GEvent.VALUE_CHANGING){
			lblValueX.setLocalColorScheme(G4P.RED_SCHEME);
			lblValueY.setLocalColorScheme(G4P.RED_SCHEME);
		}
		else {
			lblValueX.setLocalColorScheme(G4P.GREEN_SCHEME);
			lblValueY.setLocalColorScheme(G4P.GREEN_SCHEME);
		}
		lblValueX.setText("" + s0.getValueXF());
		lblValueY.setText("" + s0.getValueYF());
	}

	
	// =================================================================================
	// ===================            New pde Tab             ==========================

	public void handleSliderEvents(GValueControl slider, GEvent event) { 
		if(slider == sdrEasing)
			s0.setEasing(slider.getValueF());		
		else if(slider == sdrBack)
			bgcol = slider.getValueI();	
	}

	public void handleKnobEvents(GValueControl knob, GEvent event) { 
		if(knbAngle == knob)
			s0.setRotation(knbAngle.getValueF(), GControlMode.CENTER);
	}

	public void handleButtonEvents(GButton button, GEvent event) { 
		if(button.tagNo >= 1000)
			s0.setLocalColorScheme(button.tagNo - 1000);
	}
	

	public void handleToggleControlEvents(GToggleControl option, GEvent event) {
		if(option == cbxOpaque)
			s0.setOpaque(option.isSelected());

	}

	GCheckbox cbxOpaque;
	GSlider sdrBack, sdrEasing;
	GLabel lblEasing, lblValueX, lblValueY;
	GKnob knbAngle;
	GButton[] btnColours = new GButton[8];

	int bgcol;
	
	public void makeSliderConfigControls(){
		// Create colour scheme selectors
		int x = width - 42, y = 2;
		for(int i = 0; i < btnColours.length; i++){
			btnColours[i] = new GButton(this, x, y + i * 20, 40, 18, "" + (i+1));
			btnColours[i].tag = "Button: " + (i+1);
			btnColours[i].setLocalColorScheme(i);
			btnColours[i].tagNo = 1000+i;
		}	
		// Create sliders
		x = width-120; y = 162;
		bgcol = 200; 
		sdrBack = new GSlider(this, x, y, 162, 80, 12);
		sdrBack.setLimits(bgcol, 0, 255);
		sdrBack.setRotation(-PI/2);
		sdrBack.setTextOrientation(G4P.ORIENT_RIGHT);
		sdrBack.setEasing(20);
		sdrBack.setShowValue(true);
		sdrBack.setShowTicks(true);

		x = width - 100; y = 250;
		knbAngle = new GKnob(this, x, y, 70, 70, 0.6f);
		knbAngle.setTurnRange(0, 360);
		knbAngle.setLimits(0.0f, 0.0f, TWO_PI);
		knbAngle.setTurnMode(G4P.CTRL_ANGULAR);
		knbAngle.setIncludeOverBezel(true);
		knbAngle.setNbrTicks(13);
		knbAngle.setStickToTicks(true);

		x = width - 90; y = 152;
		cbxOpaque = new GCheckbox(this, x, y + 70 , 70, 18, "Opaque");	
		lblEasing = new GLabel(this, x, y, 80, 40, "Easing");
		lblEasing.setTextItalic();
		lblEasing.setTextBold();
		sdrEasing = new GSlider(this, x, y + 32, 80, 40, 12);
		sdrEasing.setLimits(1.0f, 1.0f, 30.0f);
		sdrEasing.setShowValue(true);
		
		x = 20; y = height - 30;
		lblValueX = new GLabel(this, x, y, 90, 20);
		lblValueX.setLocalColorScheme(G4P.GREEN_SCHEME);
		lblValueX.setTextAlign(GAlign.LEFT, null);
		lblValueX.setOpaque(true);
		lblValueY = new GLabel(this, x + 120, y, 90, 20);
		lblValueY.setLocalColorScheme(G4P.GREEN_SCHEME);
		lblValueY.setTextAlign(GAlign.LEFT, null);
		lblValueY.setOpaque(true);
	}
}
